Description
Run time attribution is a way of deferring specification of constant values from compile-time to run-time. For example we may want to design a system that can cater for any number of cars, but may not know how many until runtime.
Compile Time Constants
In the case where values are known at compile time we can simply specify an algebraic constant (Eg. MAX_NUM_CARS) (see Macro Definitions). These definitions can then be defined in an Included Header File or the project header file ProjectHeader.hpp.
In the above example we could use the following macro;
#define MAX_NUM_CARS 3
Runtime Constants
With very few exceptions however, CDL attributes can be assigned to variables that are populated at runtime prior to circuit creation/initialization. In particular object/connection dimensions are often initialized from values held in configuration files.
In this case we can still use an algebraic constant, but could define a global variable that is initialized at run-time. The variable can then be initialized before the main CLIP Process is started. To this end, the main CLIP Process function Startup(), breaks the start sequence into a number of steps (see Processes), the first of which is a Pre-Start step, which allows some processing to be scheduled before circuitry is created and initialized.
Thus to initialize the run-time constants, simply add code to the <ProcName>Process::OnPrestart() function.
Macro Definitions
In addition many parameters that take a predefined Keyword can also take Macro Definitions.
Macro Definitions can be simple numeric substitutions, nested substitutions or complex expressions.
Eg.
#define
MAX_NUM_CARS
3
#define MAX_NUM_VEHICLES
(MAX_NUM_CARS*2)
#define MAX_TRANSPORTERS
ClpNumProcessors()
#define
VEHICAL_ID
(this->ElemNum())
#define TRANSPORT_PRIO
((VEHICAL_ID > ClpNumProcessors() ) ? 1 :
0 )
#define TRANSPORT_MGR( X ) ( (
ClpThisSlaveId() == (X) ) ? TRUE : FALSE )
Macro Definitions, however complex, resolve to three basic types
- Compile-Time Constants - Values that decompose into a single compile time constant (MAX_NUM_CARS, MAX_NUM_VEHICLES)
- Run-Time Constants - Values that rely on some run-time component, but once defined are constant (MAX_TRANSPORTERS)
- Run-Time variables - Values that rely on some run-time component and can be changed at any time (VEHICAL_ID, TRANSPORT_PRIO, TRANSPORT_MGR)
Run-Time Constants can be used in most places that Constants can, however some care is required. Since Run-Time Constants are not constant at compile time, the compiler will generate compile time errors if they are used in places where true constants are required (see Run-Time Attribution).
Run-Time Variables, although valid anywhere in general code (except where a true constant is required), should not be used for Blueprint attributes that expect a true or Run-Time Constant (Eg. Object Creation or Initialization parameters - object dimensions, Initial value).
It is "good practice" to distinguish true constants from run-time constants and run-time variables. For example defined constants should be UPPER_CASE_WORDS_SEPARATED_BY_UNDERSCORES, whilst run-time constants should be defined with a prefix letter (ie g for global variable) gRUN_TIME_CONSTANT and Run-Time variables should be MixedCase.
Eg.
#define
MAX_NUM_CARS
3
#define MAX_NUM_VEHICLES
(MAX_NUM_CARS*2)
#define gMAX_TRANSPORTERS
ClpNumProcessors()
#define VehicalId
(this->ElemNum())
#define TransportPrio
((VEHICAL_ID >
ClpNumProcessors() ) ? 1 : 0 )
#define TransportMgr( X ) ((
ClpThisSlaveId() == (X) ) ? TRUE : FALSE )